package com.sky.service.impl;



import com.sky.context.BaseContext;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Orders;
import com.sky.mapper.AdminOrderMapper;
import com.sky.mapper.OrderDetailMapper;
import com.sky.service.AdminOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;

@Service
public class AdminOrderServiceImpl implements AdminOrderService {
    @Autowired
    AdminOrderMapper adminOrderMapper;
    @Autowired
    OrderDetailMapper orderDetailMapper;


    //拒绝订单及拒绝原因
    @Override
    public void rejection(OrdersRejectionDTO ordersRejectionDTO) {
        Orders orders = adminOrderMapper.findByOrderId(ordersRejectionDTO.getId());
        if (orders.getRejectionReason()==null){
            orders.setUserId(BaseContext.getCurrentId());
            BeanUtils.copyProperties(ordersRejectionDTO,orders);
        }
        //设置当前订单状态
        orders.setStatus(Orders.CANCELLED);
        orders.setCancelTime(LocalDateTime.now());
        //订单取消更新数据库
        adminOrderMapper.update(orders);
    }

    /**
     * 取消订单
     * @param ordersCancelDTO
     */
    @Override
    public void cancelAdminOrder(OrdersCancelDTO ordersCancelDTO) {
        //创建Orders对象，
        // 将ordersCancelDTO的ID和cancelReason复制到orders中
        Orders orders = new Orders();
        BeanUtils.copyProperties(ordersCancelDTO,orders);
        //设置订单状态为 6->已取消
        orders.setStatus(Orders.CANCELLED);
        orders.setCancelTime(LocalDateTime.now());
        orders.setCancelReason(ordersCancelDTO.getCancelReason());
        //根据订单的ID修改数据库中的status和cancelReason
        adminOrderMapper.updateOrder(orders);

    }


}
